//
//  OtherTableViewCell.m
//  WWTQ
//
//  Created by JP on 2018/10/30.
//  Copyright © 2018年 Jqert. All rights reserved.
//

#import "OtherTableViewCell.h"

#import "ForecastModel.h"

@interface OtherTableViewCell ()
@property (nonatomic, strong) UILabel *ganmao;
@property (nonatomic, strong) UILabel *fengLi;
@property (nonatomic, strong) UILabel *shiDu;
@property (nonatomic, strong) UILabel *riChu;
@property (nonatomic, strong) UILabel *kongQi;
@property (nonatomic, strong) UILabel *wenDu;
@property (nonatomic, strong) UILabel *pm;

@property (nonatomic, strong) UILabel *ganmaoDetail;
@property (nonatomic, strong) UILabel *fengLiDetail;
@property (nonatomic, strong) UILabel *shiDuDetail;
@property (nonatomic, strong) UILabel *riChuDetail;
@property (nonatomic, strong) UILabel *kongQiDetail;
@property (nonatomic, strong) UILabel *wenDuDetail;
@property (nonatomic, strong) UILabel *pmDetail;
@end

@implementation OtherTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    self.ganmao = [self setUpTitleLabel:@"感冒提醒（指数）"];
    self.fengLi = [self setUpTitleLabel:@"风向风力"];
    self.shiDu = [self setUpTitleLabel:@"湿度"];
    self.riChu = [self setUpTitleLabel:@"日出/日落"];
    self.kongQi = [self setUpTitleLabel:@"空气质量指数"];
//    self.pm = [self setUpTitleLabel:@"PM 2.5"];
    [self.contentView addSubview:self.ganmao];
    [self.contentView addSubview:self.fengLi];
    [self.contentView addSubview:self.shiDu];
    [self.contentView addSubview:self.riChu];
    [self.contentView addSubview:self.kongQi];
//    [self.contentView addSubview:self.pm];
    
    self.fengLi.attributedText = [self addPicture:@" 风向风力" iconName:@"fengli" index:0];
    self.shiDu.attributedText = [self addPicture:@" 湿度" iconName:@"shidu" index:0];
    self.kongQi.attributedText = [self addPicture:@" 空气质量指数" iconName:@"kongqi" index:0];
    self.pm.attributedText = [self addPicture:@" PM 2.5" iconName:@"pm" index:0];
    self.ganmao.attributedText = [self addPicture:@" 感冒提醒(指数)" iconName:@"ganmao" index:0];
    //日出
    NSMutableAttributedString *attriRiChu = [[NSMutableAttributedString alloc] initWithString:@"日出 / 日落"];
    NSTextAttachment *riLuo = [[NSTextAttachment alloc] init];
    riLuo.image = [UIImage imageNamed:@"riLuo"];
    riLuo.bounds = CGRectMake(0, 0, 18, 18);
    NSTextAttachment *riChu = [[NSTextAttachment alloc] init];
    riChu.image = [UIImage imageNamed:@"riChu"];
    riChu.bounds = CGRectMake(0, 0, 18, 18);
    NSAttributedString *riLuoStr = [NSAttributedString attributedStringWithAttachment:riLuo];
    NSAttributedString *riChuStr = [NSAttributedString attributedStringWithAttachment:riChu];
    [attriRiChu insertAttributedString:riLuoStr atIndex:5];
    [attriRiChu insertAttributedString:riChuStr atIndex:0];
    self.riChu.attributedText = attriRiChu;
    
    self.ganmaoDetail = [self setUpTitleDetailLabel:@"暂无"];
    self.fengLiDetail = [self setUpTitleDetailLabel:@"暂无"];
    self.shiDuDetail = [self setUpTitleDetailLabel:@"暂无"];
    self.riChuDetail = [self setUpTitleDetailLabel:@"暂无"];
    self.kongQiDetail = [self setUpTitleDetailLabel:@"暂无"];
//    self.pmDetail = [self setUpTitleDetailLabel:@"暂无"];
    [self.contentView addSubview:self.ganmaoDetail];
    [self.contentView addSubview:self.fengLiDetail];
    [self.contentView addSubview:self.shiDuDetail];
    [self.contentView addSubview:self.riChuDetail];
    [self.contentView addSubview:self.kongQiDetail];
//    [self.contentView addSubview:self.pmDetail];
    
    [self setUpLayoutWithMasonry];
}

- (void)setDataModel:(DataObjectModel *)dataModel {
    _dataModel = dataModel;
    
    self.ganmaoDetail.text = dataModel.ganmao;
    self.shiDuDetail.text = dataModel.shidu;
    ForecastModel *model = dataModel.forecast[0];
    self.fengLiDetail.text = [NSString stringWithFormat:@"%@%@", model.fx, model.fl];
    self.riChuDetail.text = [NSString stringWithFormat:@"%@ / %@", model.sunrise, model.sunset];
    CGFloat aqiF = [model.aqi floatValue];
    NSString *aqiStr;
    if (aqiF < 51) {
        aqiStr = @"优";
    } else if (aqiF < 101) {
        aqiStr = @"良";
    } else if (aqiF < 151) {
        aqiStr = @"轻度污染";
    } else if (aqiF < 201) {
        aqiStr = @"中度污染";
    } else if (aqiF < 301) {
        aqiStr = @"重度污染";
    } else {
        aqiStr = @"严重污染";
    }
    
    self.kongQiDetail.text = [NSString stringWithFormat:@"%@ / %@", model.aqi, aqiStr];
    
    /*
    if (dataModel.pm25.length < 1) {
        self.pmDetail.text = @"暂无";
    } else {
        self.pmDetail.text = [NSString stringWithFormat:@"%@", dataModel.pm25];
    }
     */
}

- (NSMutableAttributedString *)addPicture:(NSString *)content iconName:(NSString *)iconName index:(NSInteger)index  {
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:content];
    NSTextAttachment *textAtt = [[NSTextAttachment alloc] init];
    textAtt.image = [UIImage imageNamed:iconName];
    textAtt.bounds = CGRectMake(0, 0, 16, 16);
    NSAttributedString *attriStr = [NSAttributedString attributedStringWithAttachment:textAtt];
    [attri insertAttributedString:attriStr atIndex:index];
    return attri;
}

- (void)setUpLayoutWithMasonry {
    [self.fengLi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.centerY.equalTo(self.contentView).multipliedBy(0.5);
        make.size.mas_equalTo(CGSizeMake((ScreenW -80)/2, 18));
    }];
    [self.riChu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-20);
        make.centerY.equalTo(self.contentView).multipliedBy(0.5);
        make.size.mas_equalTo(CGSizeMake((ScreenW -80)/2, 18));
    }];
    [self.shiDu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.centerY.equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake((ScreenW -80)/2, 18));
    }];
    [self.kongQi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-20);
        make.centerY.equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake((ScreenW -80)/2, 18));
    }];
    [self.ganmao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.centerY.equalTo(self.contentView).multipliedBy(1.5);
        make.size.mas_equalTo(CGSizeMake((ScreenW -40), 18));
    }];
    /*
    [self.pm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-20);
        make.centerY.equalTo(self.contentView).multipliedBy(1.5);
        make.size.mas_equalTo(CGSizeMake((ScreenW -80)/2, 18));
    }];
     */
    
    [self.fengLiDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(self.fengLi.mas_bottom).offset(2);
        make.size.mas_equalTo(CGSizeMake((ScreenW -80)/2, 20));
    }];
    [self.riChuDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-20);
        make.top.equalTo(self.riChu.mas_bottom).offset(2);
        make.size.mas_equalTo(CGSizeMake((ScreenW -80)/2, 20));
    }];
    [self.shiDuDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(self.shiDu.mas_bottom).offset(2);
        make.size.mas_equalTo(CGSizeMake((ScreenW -80)/2, 20));
    }];
    [self.kongQiDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-20);
        make.top.equalTo(self.kongQi.mas_bottom).offset(2);
        make.size.mas_equalTo(CGSizeMake((ScreenW -80)/2, 20));
    }];
    [self.ganmaoDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(self.ganmao.mas_bottom).offset(2);
        make.size.mas_equalTo(CGSizeMake((ScreenW -40), 20));
    }];
    /*
    [self.pmDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-20);
        make.top.equalTo(self.pm.mas_bottom).offset(2);
        make.size.mas_equalTo(CGSizeMake((ScreenW -80)/2, 20));
    }];
     */
}

#pragma mark - titleLabel
- (UILabel *)setUpTitleLabel:(NSString *)title {
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.textColor = RGB(220,220,220);
    titleLabel.font = ZYT14Font;
    return titleLabel;
}

#pragma mark - titleDetailLabel
- (UILabel *)setUpTitleDetailLabel:(NSString *)title {
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.textColor = [UIColor whiteColor];
//    titleLabel.font = [UIFont fontWithName:@"AppleGothic" size:15];
    titleLabel.font = ZYT15Font;
    return titleLabel;
}

@end
